package com.stox.client.ui.javafx.chartingy.event.listener;

import javafx.scene.input.MouseEvent;

import com.stox.client.ui.javafx.chartingy.model.chart.Chart;
import com.stox.client.ui.javafx.chartingy.model.shape.HorizontalLine;
import com.stox.client.ui.javafx.chartingy.view.ChartingView;
import com.stox.shared.core.model.intf.Callback;

public class HorizontalLineMouseEventStrategy implements ChartingMouseEventStrategy {

	private Callback<Void, Void> completionCallback;

	@Override
	public void setCompletionCallback(final Callback<Void, Void> completionCallbak) {
		completionCallback = completionCallbak;
	}

	@Override
	public void handle(final MouseEvent event) {
		final ChartingView chartingView = (ChartingView) event.getSource();
		final Chart<?, ?> chart = chartingView.resolveChart(event.getSceneX(), event.getSceneY());

		if (null != chart) {
			final HorizontalLine line = new HorizontalLine(chart, event.getSceneY());
			chart.addShape(line);
			if (null != completionCallback) {
				completionCallback.execute(null);
			}
		}

	}

	@Override
	public void attach(final ChartingView chartingView) {
		chartingView.addEventFilter(MouseEvent.MOUSE_PRESSED, this);
	}

	@Override
	public void detach(final ChartingView chartingView) {
		chartingView.removeEventFilter(MouseEvent.MOUSE_PRESSED, this);
	}

}
